﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace CslData
{
    /// <summary>
    /// 解析器
    /// </summary>
    public class Parsers
    {
        public static string DownloadStringWithLimitedTime(string url, int limit = 1)
        {
            List<Task> tasks = new List<Task>();
            WebClient client = new WebClient();
            string finalResult = null;
            if (limit > 0)
            {
                var task = client.DownloadStringTaskAsync(new Uri(url));
                tasks.Add(task);
                client.DownloadStringCompleted += (a, b) =>
                {
                    if (b.Error == null)
                    {
                        finalResult = b.Result;
                        //return b.Result;
                    }
                    else
                    {
                        finalResult = DownloadStringWithLimitedTime(url, limit - 1);
                    }
                };
                Task.WaitAll(tasks.ToArray());
                //task.Wait();
                return finalResult;
            }
            else
            {
                throw new Exception("Download Times Limit exceeded.");
            }
        }

        /// <summary>
        /// 根据实际url和轮数获得比赛信息列表
        /// </summary>
        /// <param name="url"></param>
        /// <param name="round"></param>
        /// <returns></returns>
        public static List<MatchInfo> GetMatchInfosByActualUrl(string url, int round)
        {
            WebClient client = new WebClient();
            var resultString = GetMatchInfosStringByActualUrl(url);
            var list = JsonConvert.DeserializeObject<List<MatchInfo>>(resultString);
            return list;
        }
        public static string GetMatchInfosStringByActualUrl(string url)
        {
            WebClient client = new WebClient();
            Trace.WriteLine(client.Encoding.WebName);
            var downloadedBytes = client.DownloadData(new Uri(url));
            Console.WriteLine($"{url} downloaded");
            var utf8String = Encoding.UTF8.GetString(downloadedBytes);
            Trace.WriteLine(utf8String.Length);
            var asciiString = Encoding.ASCII.GetString(downloadedBytes);
            Trace.WriteLine(asciiString.Length);
            var gbkString = Encoding.GetEncoding("GBK").GetString(downloadedBytes);
            Trace.WriteLine(gbkString.Length);
            string sss = "新时代";


            var max = sss.Max(ch => ((Int64)ch));

            var chh = (Char)max;

            char ch1 = sss[2];

            var i64 = (Int64)ch1;
            return utf8String;
        }

        /// <summary>
        ///  根据url模板和轮数获得比赛信息列表
        /// </summary>
        /// <param name="urlTemplate"></param>
        /// <param name="round"></param>
        /// <returns></returns>
        public static List<MatchInfo> GetMatchInfosByUrlTemplate(string urlTemplate, int round)
        {
            string url = String.Format(urlTemplate, round);
            return GetMatchInfosByActualUrl(url, round);
        }

        public static List<MatchInfo> GetMatchInfosByJsonString(string json)
        {
            var list = JsonConvert.DeserializeObject<List<MatchInfo>>(json);
            //var xml = JsonConvert.DeserializeXmlNode(json);
            return list;
            //throw new NotImplementedException();
        }

    }

}
